package com.lagou;

import com.lagou.bean.ConsumerComponent;
import com.lagou.filter.TPMonitorFilter;
import com.lagou.util.Statistics;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class AnnotationConsumerMain {
    public static void main(String[] args) throws IOException, InterruptedException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        ConsumerComponent service = context.getBean(ConsumerComponent.class);

        ExecutorService executorService = Executors.newFixedThreadPool(3);
        //调用接口一
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                while (true){
                    String s = service.sayHello1("hello1 ...", 0);
                    //System.out.println("result :" + s);
                }
            }
        });

        //调用接口二
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                while (true){
                    String s = service.sayHello2("hello2 ...", 0);
                    //System.out.println("result :" + s);
                }
            }
        });

        //调用接口三
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                while (true){
                    String s = service.sayHello3("hello3 ...", 0);
                    //System.out.println("result :" + s);
                }
            }
        });

         ScheduledExecutorService scht = Executors.newScheduledThreadPool(3);
        //开始任务调度
        scht.scheduleWithFixedDelay(new Statistics(TPMonitorFilter.queue1,"sayHello1"), 5000L, 5000L, TimeUnit.MILLISECONDS);
        scht.scheduleWithFixedDelay(new Statistics(TPMonitorFilter.queue2,"sayHello2"), 5000L, 5000L, TimeUnit.MILLISECONDS);
        scht.scheduleWithFixedDelay(new Statistics(TPMonitorFilter.queue3,"sayHello3"), 5000L, 5000L, TimeUnit.MILLISECONDS);
        Thread.sleep(Long.MAX_VALUE);
    }

    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    //@EnableDubbo(scanBasePackages = "com.lagou.bean")
    @ComponentScan("com.lagou.bean")
    @EnableDubbo
    static class ConsumerConfiguration {

    }

}
